package com.dsteiner.widgets.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.ISimplePanel;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.resources.NineBoxCssResource;
import com.dsteiner.widgets.client.resources.RoundBorderCssResource;
import com.dsteiner.widgets.client.util.CssUtil;

public class RoundBorderController {

	private RoundBorderModel roundBorderModel;
	private ISimplePanel contentPanelHolder;
	private ILazyWidget roundBorder;

	public void registerContentPanelHolder(ISimplePanel contentPanelHolder) {
		this.contentPanelHolder = contentPanelHolder;
	}
	public void registerRoundBorderModel(RoundBorderModel nineBoxModel) {
		this.roundBorderModel = nineBoxModel;
		nineBoxModel.addPropertyChangeListener("cssResource", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateStyleNames();
			}
		});
		nineBoxModel.addPropertyChangeListener("contentWidget", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateContentWidget();
			}
		});
	}
	public void registerRoundBorder(ILazyWidget nineBoxWidget) {
		this.roundBorder = nineBoxWidget;
		nineBoxWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateContentWidget();
				updateStyleNames();
			}
		});
	}
	private void updateContentWidget() {
		IWidget contentWidget = roundBorderModel.getContentWidget();
		contentPanelHolder.setWidget(contentWidget);
	}

	private void updateStyleNames() {
		RoundBorderCssResource nineBoxCssResource = roundBorderModel.getCssResource();
		contentPanelHolder.setStyleName(nineBoxCssResource.contentPanelHolder());
		String roundBorderStyleName = nineBoxCssResource.roundBorder();
		roundBorder.setStyleName(roundBorderStyleName);
		if(roundBorderModel.getStyleNames() != null)
			roundBorder.addStyleName(roundBorderModel.getStyleNames());
	}
}